﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using KouakKore;

namespace Kouakos.ViewModel
{
    class MainViewModel : ViewModelBase
    {
        #region TitleProperty
        private const string TitlePropertyName = "Title";
        private string _Title = "KouakOS - HTTP File Share Server";
        public string Title
        {
            get { return this._Title; }
            set
            {
                if (value != this._Title)
                {
                    this._Title = value;
                    RaisePropertyChanged(TitlePropertyName);
                }
            }
        }
        #endregion

        #region SubNameProperty
        private const string SubNamePropertyName = "SubName";
        private string _SubName = "HTTP File Share Server";
        public string SubName
        {
            get { return this._SubName; }
            set
            {
                if (value != this._SubName)
                {
                    this._SubName = value;
                    RaisePropertyChanged(SubNamePropertyName);
                }
            }
        }
        #endregion

        #region NameProperty
        private const string NamePropertyName = "Name";
        private string _Name = "KouakOS";
        public string Name
        {
            get { return this._Name; }
            set
            {
                if (value != this._Name)
                {
                    this._Name = value;
                    RaisePropertyChanged(NamePropertyName);
                }
            }
        }
        #endregion

        #region PathProperty
        private const string pathPropertyName = "Path";
        private string _Path = "";
        public string Path
        {
            get { return this._Path; }
            set
            {
                if (value != this._Path)
                {
                    this._Path = value;
                    RaisePropertyChanged(pathPropertyName);
                }
            }
        }
        #endregion

        #region LocalIPProperty
        private const string LocalIPPropertyName = "LocalIP";
        private string _LocalIP = KouakKore.KouakIP.GetLocalIpAdress().ToString();
        public string LocalIP
        {
            get { return this._LocalIP; }
            set
            {
                if (value != this._LocalIP)
                {
                    this._LocalIP = value;
                    RaisePropertyChanged(LocalIPPropertyName);
                }
            }
        }
        #endregion

        #region PublicIPProperty
        private const string PublicIPPropertyName = "PublicIP";
        private string _PublicIP = KouakKore.KouakIP.GetExternalIp().ToString();
        public string PublicIP
        {
            get { return this._PublicIP; }
            set
            {
                if (value != this._PublicIP)
                {
                    this._PublicIP = value;
                    RaisePropertyChanged(PublicIPPropertyName);
                }
            }
        }
        #endregion

        #region InitProperty
        private const string InitPropertyName = "Init";
        private bool _Init = true;
        public bool Init
        {
            get { return this._Init; }
            set
            {
                if (value != this._Init)
                {
                    this._Init = value;
                    RaisePropertyChanged(InitPropertyName);
                }
            }
        }
        #endregion

        #region RunProperty
        private const string RunPropertyName = "Run";
        private bool _Run = false;
        public bool Run
        {
            get { return this._Run; }
            set
            {
                if (value != this._Run)
                {
                    this._Run = value;
                    RaisePropertyChanged(RunPropertyName);
                }
            }
        }
        #endregion

        private bool IsFile;
        private KouakLauncher k;
        public ICommand SelectDir { get; internal set; }
        public ICommand SelectFile { get; internal set; }
        public ICommand Launch { get; internal set; }
        public ICommand StopServer { get; internal set; }

        public MainViewModel()
        {
            SelectDir = new RelayCommand(() => { SelectDirectoryAction(); }, () => { return true; });
            SelectFile = new RelayCommand(() => { SelectFileAction(); }, () => { return true; });
            Launch = new RelayCommand(() => { LaunchAction(); }, () => { return true; });
            StopServer = new RelayCommand(() => { k.Stop(); Run = false; Init = true; }, () => { return true; });
        }


        private void SelectDirectoryAction()
        {
            using (System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog())
            {
                dlg.Description = "Select a folder";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    IsFile = false;
                    this.Path = dlg.SelectedPath + "\\";
                }
            }
        }

        private void SelectFileAction()
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                IsFile = true;
                this.Path = openFileDialog.FileName;
            }
        }

        private void LaunchAction()
        {
            if (IsFile)
            {
                string[] toSendArgs = { "-f", "-a", "-p", "\"" + Path + "\"" };
                k = new KouakLauncher();
                k.Launch(toSendArgs);
            }
            else
            {
                string[] toSendArgs = { "-d", "-a", "-p", "\"" + Path + "\"" };
                k = new KouakLauncher();
                k.Launch(toSendArgs);
            }
            this.Init = false;
            this.Run = true;
        }
    }
}
